﻿using Ping9719.DeviceCommunication.Common.Helpers;
using Ping9719.DeviceCommunication;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlTypes;
using System.Runtime.InteropServices;

namespace BaseUi.Device
{
    public class ChangeBan : Ping9719.DeviceCommunication.SerialPortBase
    {
        /// <summary>
        /// 比较两个字节数组是否相等
        /// </summary>
        /// <param name="b1">byte数组1</param>
        /// <param name="b2">byte数组2</param>
        /// <returns>是否相等</returns>
        private bool PasswordEquals(byte[] b1, byte[] b2)
        {
            if (b1 == null || b2 == null) return false;
            if (b1.Length != b2.Length) return false;
            for (int i = 0; i < b1.Length; i++)
                if (b1[i] != b2[i])
                    return false;
            return true;
        }
        //int number = 7903376;
        //string ss = number.ToString("X");
        /// <summary>
        /// 使用串口的方式
        /// </summary>
        public ChangeBan(string portName, int baudRate = 9600, Parity parity = Parity.None, int dataBits = 8, StopBits stopBits = StopBits.One)
        {
            if (serialPort == null)
                serialPort = new SerialPort();
            serialPort.PortName = portName;
            serialPort.BaudRate = baudRate;
            serialPort.DataBits = dataBits;
            serialPort.StopBits = stopBits;
            serialPort.Encoding = Encoding.UTF8;
            serialPort.Parity = parity;
        }
        /// <summary>
        ///发送进入页面可切换模式
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan1(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xEF, 0x01,0x5B };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回进入页面可切换模式
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan1(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xEF, 0x03, 0x00, 0xB0, 0x5B };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///发送巡检指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan2(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x01, 0xD0 };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回巡检指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan2(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x03, 0x00, 0x90, 0xD0 };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }

        /// <summary>
        ///发送全亮指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan3(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x01, 0xC9 };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回全亮指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan3(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x03, 0x00, 0x90, 0xC9};
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///发送显示444444指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan4(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x01, 0xCA };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回显示444444指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan4(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x03, 0x00, 0x90, 0xCA };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///发送显示555555指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan5(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x01, 0xCB };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回显示555555指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan5(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x03, 0x00, 0x90, 0xCB };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///发送%%%%%%
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan6(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x01, 0xCC };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回%%%%%%
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan6(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x03, 0x00, 0x90, 0xCC };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///发送显示......指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan7(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x01, 0xCD };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回显示......指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan7(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x03, 0x00, 0x90, 0xCD };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///发送显示222222指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan8(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x01, 0xCE };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回显示222222指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan8(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x03, 0x00, 0x90, 0xCE };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///发送全熄屏指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan9(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x01, 0xCF };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回全熄屏指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan9(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x03, 0x00, 0x90, 0xCF };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///发送LCD DOT TEST指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Setchan10(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x82, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x01, 0xC8 };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        ///返回LCD DOT TEST指令
        /// </summary>
        /// <param name="addr">安普板地址</param>
        /// <returns></returns>
        public static byte[] Getchan10(int addr)
        {
            string ss = addr.ToString("X");
            int add1 = Convert.ToInt32(ss.Substring(0, 2), 16);
            int add2 = Convert.ToInt32(ss.Substring(2, 2), 16);
            int add3 = Convert.ToInt32(ss.Substring(4, 2), 16);
            List<int> bytes = new List<int> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x86, 0x37, 0x5C, add1, add2, add3, 0xE5, 0x03, 0x00, 0x90, 0xC8 };
            var order = bytes.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetBCC(order);
        }
        /// <summary>
        /// 切换页面
        /// </summary>
        /// <param name="of">list<int>输入内容</param>
        /// <returns></returns>
        public static byte[] SetChanneall(List<int> of)
        {
            var order = of.ConvertAll<byte>(it => Convert.ToByte(it)).ToArray();
            return Ping9719.DeviceCommunication.Common.Helpers.CRC16.GetCRC16(order);
        }
        public Result<string> Readsent(int timeout, byte[] send)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                //var send = SetChanneall(of);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (send.Length+1 != aaa.Value.Length)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "长度错误"+ Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                if (!CRC16.CheckCRC16(aaa.Value))
                {
                    aaa.IsSucceed = false;
                    aaa.Value = new byte[] { };
                    aaa.Err = "数据CRC16验证不合格" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                for (int i = 0; i < aaa.Value.Length; i++)
                {
                    result.Value += aaa.Value[i].ToString("X2");
                    if (i < 9 && aaa.Value[i] != send[i])
                    {
                        aaa.IsSucceed = false;
                        aaa.Err = result.Value + "错误";
                        return new Result<string>(aaa);
                    }
                    
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入页面可切换模式
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent1(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan1(addr);
                var gend = Getchan1(addr);
                string ss = "";
                string ss1 = "";
                for (int i = 0; i < gend.Length; i++)
                {
                    ss += gend[i].ToString("X2") + " ";
                }
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                for (int i = 0; i < aaa.Value.Length; i++)
                {
                    ss1 += aaa.Value[i].ToString("X2") + " ";
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + ss1;
                    return new Result<string>(aaa);
                }
                result.Value = ss1;
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入巡检指令
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent2(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan2(addr);
                var gend = Getchan2(addr);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入全亮指令
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent3(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan3(addr);
                var gend = Getchan3(addr);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入显示444444指令
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent4(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan4(addr);
                var gend = Getchan4(addr);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入显示555555指令
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent5(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan5(addr);
                var gend = Getchan5(addr);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入显示%%%%%%指令
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent6(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan6(addr);
                var gend = Getchan6(addr);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入显示......指令
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent7(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan7(addr);
                var gend = Getchan7(addr);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入显示222222指令
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent8(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan8(addr);
                var gend = Getchan8(addr);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入全熄屏指令
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent9(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan9(addr);
                var gend = Getchan9(addr);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
        /// <summary>
        /// 进入LCD DOT TEST指令
        /// </summary>
        /// <param name="timeout"></param>
        /// <param name="send"></param>
        /// <returns></returns>
        public Result<string> Readsent10(int timeout, int addr)
        {
            var conn = Connect1();
            if (!conn.IsSucceed)
                return new Result<string>(conn);
            serialPort.ReadTimeout = timeout;
            serialPort.WriteTimeout = timeout;
            var result = new Result<string>();
            try
            {
                //清空字符,还原状态
                serialPort.DiscardInBuffer();
                var send = Setchan10(addr);
                var gend = Getchan10(addr);
                serialPort.Write(send, 0, send.Length);
                var aaa = SerialPortRead();
                if (!aaa.IsSucceed)
                    return new Result<string>(aaa);
                if (aaa.Value == null|| aaa.Value.Length==0)
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "无信息";
                    return new Result<string>(aaa);
                }
                if (!PasswordEquals(aaa.Value, gend))
                {
                    aaa.IsSucceed = false;
                    aaa.Err = "返回信息错误" + Encoding.Default.GetString(aaa.Value);
                    return new Result<string>(aaa);
                }
                result.Value = Encoding.Default.GetString(aaa.Value);
            }

            catch (Exception ex)
            {
                result.IsSucceed = false;
                result.Err = ex.Message;
            }
            return result.EndTime();
        }
    }
}
